package cn.hotel.service.impl;

import cn.hotel.entity.User;
import cn.hotel.mapper.UserMapper;
import cn.hotel.service.UserService;
import cn.hotel.utils.EMS;
import cn.hotel.utils.MD5Utils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import java.util.Random;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User queryUserById(String userId) {
        return userMapper.queryUserById(userId);
    }

    @Override
    public User queryUserByUserAndPass(String username, String password) {
        //对密码进行加密
        password = MD5Utils.MD5Encode(password);
        return userMapper.queryUserByUserAndPass(username,password);
    }

    @Override
    public int saveUser(User user) {
        String password = MD5Utils.MD5Encode(user.getPassword());
        user.setPassword(password);
        return userMapper.addUser(user);
    }

    /**
     * 获取用户的数量
     * @return
     */
    @Override
    public int getUsers() {
        return userMapper.getUsers();
    }

    @Override
    public int getCode(String phone,int code) {
        try {
            EMS.getCheckWord(phone,code);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int changePassword(String phone,String password) {
        //对密码进行加密
        password=MD5Utils.MD5Encode(password);
        return userMapper.updatePassword(phone, password);
    }

    @Override
    public List<User> allUser() {
        List<User> users = userMapper.allUser();
        return users;
    }

    @Override
    public int deleteByUserId(Integer userId) {
        int i = userMapper.deleteByUserId(userId);
        return i;
    }

    @Override
    public int userCount() {
        int i = userMapper.userCount();
        return i;
    }

    @Override
    public User queryById(Integer userId) {
        User user = userMapper.queryById(userId);
        return user;
    }

    @Override
    public int updateUser(User user) {
        int i = userMapper.updateUser(user);
        return i;
    }

    @Override
    public List<User> getUserByName(String name) {
        List<User> userByName = userMapper.getUserByName(name);
        return userByName;
    }
}
